package onion.molingo2o.fragment;

import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import onion.molingo2o.R;
import onion.molingo2o.views.LoadingDialog;

/**
 * Created by Administrator on 2015/12/19.
 */
public class BaseFragment extends Fragment {
    ImageView ivBack,ivRight;
    TextView tvLeft,tvRight,tvTitle;
    protected LoadingDialog mLdDialog = null;
    protected View.OnClickListener topClickListener;
    private void InitDialog() {
        mLdDialog = new LoadingDialog(getActivity());
        // mLdDialog.setMessage(this.getString(R.string.dialog_loading_msg));
        mLdDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                dialog.cancel();
                return false;
            }
        });
    }
    protected void initTitle(String left, String title, String right) {
        if(getActivity().findViewById(R.id.heaptop)==null)return;
        ivBack = (ImageView) getActivity().findViewById(R.id.iv_back);
        tvLeft = (TextView) getActivity().findViewById(R.id.tv_left_text);
        tvRight = (TextView) getActivity().findViewById(R.id.tv_right_text);
        tvTitle = (TextView) getActivity().findViewById(R.id.tv_title);
        if (TextUtils.isEmpty(left)  ) {
            ivBack.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                   getActivity(). finish();
                }
            });
        } else {
            ivBack.setVisibility(View.GONE);
            tvLeft.setVisibility(View.VISIBLE);
            tvLeft.setText(left);
            tvLeft.setOnClickListener(topClickListener);
        }
        if (title != null) {
            tvTitle.setText(title);
        }
        if (right != null&&!right.isEmpty()) {
            tvRight.setText(right);
            tvRight.setOnClickListener(topClickListener);
        }
    }
    protected void initTitle(String left, String title, int resId){
        if(getActivity().findViewById(R.id.heaptop)==null)return;
        ivRight=(ImageView) getActivity().findViewById(R.id.iv_right);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(resId);
        ivRight.setOnClickListener(topClickListener);
        initTitle(left, title, "");
    }
    protected void initTitle(int leftResId, String title, int resId){
        ivBack = (ImageView) getActivity().findViewById(R.id.iv_back);
        tvLeft = (TextView) getActivity().findViewById(R.id.tv_left_text);
        tvTitle = (TextView) getActivity().findViewById(R.id.tv_title);
        ivRight=(ImageView) getActivity().findViewById(R.id.iv_right);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(resId);
        ivBack.setImageResource(leftResId);
        tvTitle.setText(title);
    }
    protected void showToast(int strid) {
        Toast.makeText(getActivity(), strid, Toast.LENGTH_SHORT).show();
    }

    protected void showToastLong(int strid) {
        Toast.makeText(getActivity(), strid, Toast.LENGTH_LONG).show();
    }
    protected void showToast(String str) {
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }
}
